/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'promotion_timelimits',
                model: {
                    url: _global.url.api + 'promotion_timelimit',
                    event: {
                        '.item-remove:click': 'remove',
                        '.item-show:click': 'show'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    show: function (e) {
                        e.preventDefault();
                        _this.data = this.data;
                        dialog({
                            title: '查看定时器',
                            content: template('show-tpl', this.data),
                            width: 700,
                            quickClose: true,
                            ok: function () {
                                var data = {};
                                data.goods_id = $("#goods_id_edit").val();
                                data.promotion_price = $("#promotion_price_edit").val();
                                data.origin_price = $("#origin_price_edit").val();
                                data.started_at = $("#started_at_edit").val();
                                data.expired_at = $("#expired_at_edit").val();
                                data.promotion_timelimit_id = _this.data.id;
                                console.log(data);
                                if (data.started_at == '' || data.expired_at == '') {
                                    Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                    return false;
                                }
                                if (data.start_at == '' || data.end_at == '') {
                                    Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                    return false;
                                }
                                var date1 = new Date(Date.parse(data.started_at));
                                var date2 = new Date(Date.parse(data.expired_at));
                                if (date1.getTime() > date2.getTime()) {
                                    Alert({msg: '活动开始时间不能大于结束时间', type: 'error'});
                                    return false;
                                }
                                skus = $("input[name='sku_promotion_price_edit']");
                                data.skus = [];
                                $.each(skus, function (index) {
                                    promotion_price = $(this).val();
                                    id = $(this).attr('data');
                                    goods_sku_id = $(this).attr('sku-id');
                                    origin_price = $('[name=origin_price]').eq(index).val();
                                    data.skus.push({
                                        promotion_price: promotion_price,
                                        id: id,
                                        goods_sku_id: goods_sku_id,
                                        origin_price: origin_price
                                    });
                                });
                                console.log(data);
                                if (new Model({
                                        url: _global.url.api + 'promotion_timelimit',
                                        data: {'data': data}
                                    }).put()) {
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            $('body').on('click', '.long_date', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.goods_id = $('input[name="goods_id"]').val();
                _this.collection.reload();
            });
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加定时器',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {

                        if ($('#origin_price').val() == undefined) {
                            Alert({msg: '请填写商品id进行搜索', type: 'error'});
                            return false;
                        }
                        _this.data.site_id = $("#site_id").val();
                        _this.data.goods_id = $("#goods_id").val();
                        _this.data.origin_price = $("#origin_price").val();
                        _this.data.promotion_price = $("#promotion_price").val();
                        _this.data.started_at = $("#started_at").val();
                        _this.data.expired_at = $("#expired_at").val();
                        _this.data.start_at = $("#start_at").val();
                        _this.data.end_at = $("#end_at").val();
                        if (_this.data.promotion_price == '') {
                            Alert({msg: '请填写商品现价', type: 'error'});
                            return false;
                        }

                        if (_this.data.started_at == '' || _this.data.expired_at == '') {
                            Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                            return false;
                        }
                        var date1 = new Date(Date.parse(_this.data.started_at));
                        var date2 = new Date(Date.parse(_this.data.expired_at));
                        if (date1.getTime() > date2.getTime()) {
                            Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                            return false;
                        }
                        var dates1 = new Date(Date.parse(_this.data.start_at));
                        var dates2 = new Date(Date.parse(_this.data.end_at));
                        if (dates1.getTime() > dates2.getTime()) {
                            Alert({msg: '价格锁定开始时间不能大于结束时间', type: 'error'});
                            return false;
                        }

                        if (date2.getTime() > dates2.getTime()) {
                            Alert({msg: '价格锁定结束时间必须大于SKU结束时间', type: 'error'});
                            return false;
                        }

                        skus = $("input[name='sku_promotion_price']");
                        _this.data.skus = [];
                        $.each(skus, function (e) {
                            promotion_price = $(this).val();
                            id = $(this).attr('data');
                            origin_price = $(this).attr('data-origin-price');

                            _this.data.skus.push({
                                promotion_price: promotion_price,
                                id: id,
                                origin_price: origin_price
                            });
                        });

                        if (new Model({
                                url: _global.url.api + 'promotion_timelimit',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('#btn-goods-search').on('click', function (e) {
                    goods_id = $('#keyword').val();
                    if (goods_id == '') {
                        Alert({msg: '请填写商品id进行搜索', type: 'error'});
                        return false;
                    }
                    $.ajax({
                        url: _global.url.api + 'promotion_search_goods',
                        data: {goods_id: goods_id},
                        type: 'get',
                        async: false,
                        success: function (res) {
                            if (res.code == 0) {
                                $('#add_form').html(template('edit-tpl', res.data));
                                $('.long_date').on('click', function () {
                                    WdatePicker({
                                        el: this.id,
                                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                                    });
                                });
                            } else {
                                Alert({msg: res.msg, type: 'error'});
                                return false;
                            }
                        }
                    });
                });
                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
            });

        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();
});